package org.zgr.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import common.dtos.LoginDto;
import common.dtos.RegisterDto;
import common.dtos.ResponseResult;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiResponse;
//import io.swagger.annotations.ApiResponses;
import common.enums.HttpCodeEnum;
import common.utils.CookieUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.zgr.service.CaptchaService;
import org.zgr.service.UserServiceImpl;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.UUID;

/**
 * @Author: 郑国荣
 * @Date: 2023-12-31-18:40
 * @Description:
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private CaptchaService captchaService;

    @SaIgnore
    @PostMapping("/login")
//    @ApiOperation("登录")
//    @ApiResponses({@ApiResponse(code = 200, message = "登录成功"), @ApiResponse(code = 2, message = "用户名或密码错误"), @ApiResponse(code = 3, message = "请勿重复登录")})
    public ResponseResult login(@RequestBody @Validated LoginDto loginDto,HttpServletRequest request) {
        String satoken = CookieUtil.getCookie(request, "satoken");
        if (satoken == null) {
            return new ResponseResult(HttpCodeEnum.TOKEN_EXPIRE, "请重新获取验证码");
        }
        ResponseResult responseResult = captchaService.checkCaptcha(satoken, loginDto.getCaptcha());
        if (responseResult.getCode() != 2000) {
            return responseResult;
        }

        String ip = request.getRemoteAddr();
        loginDto.setUpdateIp(ip);
        return userService.login(loginDto,satoken);
    }


    @SaIgnore
    @PostMapping("/register")
//    @ApiOperation("注册")
//    @ApiResponses({@ApiResponse(code = 200, message = "注册成功"), @ApiResponse(code = 2, message = "注册失败")})
    public ResponseResult register(@RequestBody @Validated RegisterDto registerDto, HttpServletRequest request) {
        String satoken = CookieUtil.getCookie(request, "satoken");
        if (satoken == null) {
            return new ResponseResult(HttpCodeEnum.TOKEN_EXPIRE, "请重新获取验证码");
        }
        ResponseResult responseResult = captchaService.checkCaptcha(satoken, registerDto.getCaptcha());
        if (responseResult.getCode() != 2000) {
            return responseResult;
        }
        String ip = request.getRemoteAddr();
        registerDto.setRegisterIp(ip);
        return userService.register(registerDto);
    }

    @SaIgnore
    @GetMapping("/get_captcha")
    public void getCaptcha(HttpServletResponse response, HttpServletRequest request) throws IOException {
        String satoken = CookieUtil.getCookie(request, "satoken");
        if (satoken == null) {
            satoken = UUID.randomUUID().toString();
            CookieUtil.setCookie(request, response, "satoken", satoken, 60 * 60 * 24);
        }
        BufferedImage image = captchaService.getCaptcha(satoken);
        response.setContentType("image/jpeg");
        OutputStream os = response.getOutputStream();
        ImageIO.write(image, "png", os);
    }

    @SaIgnore
    @PostMapping("/login_out")
//    @ApiOperation("退出登录")
//    @ApiResponses({@ApiResponse(code = 200, message = "退出登录成功"), @ApiResponse(code = 2, message = "退出登录失败")})
    public ResponseResult loginOut(@Validated @RequestParam("id") int id) {
        return userService.loginOut(id);
    }
}
